package org.springsocial.domain;

import static ch.lambdaj.Lambda.on;
import static ch.lambdaj.Lambda.sort;

import java.io.Serializable;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.codehaus.jackson.annotate.JsonBackReference;
import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.annotate.JsonManagedReference;
import org.springsource.util.OrderExpression;

@Entity
@Table(name = "player")
public class Player implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = -3594839452655715334L;
	@Id
	@Column(name = "id",unique=true)
	private Long id;

	@Column(name = "firstName")
	private String firstName;

	@Column(name = "middleName")
	private String middleName;

	@Column(name = "lastName")
	private String lastName;

	@Temporal(TemporalType.DATE)
	@Column(name = "dateOfBirth")
	private Date dateOfBirth;

	@JsonIgnore
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "nationality", insertable = false, updatable = false)
	private Country nationality;

	@JsonIgnore
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "countryOfBirth", insertable = false, updatable = false)
	private Country countryOfBirth;

	@Column(name = "height")
	private Integer height;

	@Column(name = "weight")
	private Integer weight;

	@Column(name = "orderExpression")
	private Short orderExpression;

	@JsonBackReference(value="team")
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "team", insertable = true, updatable = true)
	private Team team;
	
	@JsonIgnore
	@JsonManagedReference("tweet")
	@OneToMany(fetch = FetchType.LAZY)
	@JoinColumn(name = "fromUserId",insertable = false, updatable = false)
	private Set<ExtendedTweet> tweets;
	
	@Column(name = "activeTweeter")
	private Boolean activeTweeter;

	public Player() {
		super();
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getMiddleName() {
		return middleName;
	}

	public void setMiddleName(String middleName) {
		this.middleName = middleName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public Date getDateOfBirth() {
		return dateOfBirth;
	}

	public void setDateOfBirth(Date dateOfBirth) {
		this.dateOfBirth = dateOfBirth;
	}

	public Country getNationality() {
		return nationality;
	}

	public void setNationality(Country nationality) {
		this.nationality = nationality;
	}

	public Country getCountryOfBirth() {
		return countryOfBirth;
	}

	public void setCountryOfBirth(Country countryOfBirth) {
		this.countryOfBirth = countryOfBirth;
	}

	public Integer getHeight() {
		return height;
	}

	public void setHeight(Integer height) {
		this.height = height;
	}

	public Integer getWeight() {
		return weight;
	}

	public void setWeight(Integer weight) {
		this.weight = weight;
	}

	public Team getTeam() {
		return team;
	}

	public void setTeam(Team team) {
		this.team = team;
	}

	public Short getOrderExpression() {
		return orderExpression;
	}

	public void setOrderExpression(Short orderExpression) {
		this.orderExpression = orderExpression;
	}

	public Set<ExtendedTweet> getTweets() {
		return tweets;
	}

	public void setTweets(Set<ExtendedTweet> tweets) {
		this.tweets = tweets;
	}

	public List<ExtendedTweet> getOrderedTweets(OrderExpression exp) {
		List<ExtendedTweet> sort = sort(tweets, on(ExtendedTweet.class)
				.getCreatedAt());
		switch (exp) {
		case DESC: {
			Collections.reverse(sort);
			return sort;
		}
		case ASC: {
			return sort;
		}

		default:
			return sort;
		}
	}

	public Boolean getActiveTweeter() {
		return activeTweeter;
	}

	public void setActiveTweeter(Boolean activeTweeter) {
		this.activeTweeter = activeTweeter;
	}

}
